package com.htjc.integral.conversion.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.htjc.integral.conversion.conf.RedisTem;
import com.htjc.integral.conversion.service.IAppMallAndElectroBindService;
import com.htjc.integral.conversion.service.IEcCorporationBoundService;
import com.htjc.integral.conversion.tokenUtil.TokenUtil;
import com.htjc.integral.conversion.util.ResponseUtil;
import com.htjc.integral.model.conversion.EcBpMemberDetail;
import com.htjc.integral.model.conversion.EcBpMemberflow;

import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;

/**
 * @author wanght
 *
 */
@Slf4j(topic = "conversionLogger")
@Service
public class AppMallAndElectroBindService implements IAppMallAndElectroBindService{


	@Autowired
	private IEcCorporationBoundService ecCorporationBoundService;
	
	@Autowired
	private RedisTem redisTem;
	
	/**
	 * @param token
	 *  		token
	 * @param jsonpCallback
	 * 			回传json串
	 * @param jsonStr
	 * 			json字符串
	 * @return
	 *  		jsonpCallback + "(" + jsonResult + ")"
	 */
	@Override
	public String listBound(String token, String jsonpCallback, String jsonStr) {
		long startTime = System.currentTimeMillis();
		JSONObject result = new JSONObject();
		log.info("AppMallAndElectroBindService->token="+token);
//		String partnerId = TokenUtil.getInstance(redisTem).getUserIdByToken(token);
		String partnerId = "06902692";//先使用固定值测试============aaa====================
		log.info("通过token获取的partnerId为：{}",partnerId);
		if (StringUtils.isBlank(partnerId)) {
			result.put("rcode", "1204");
			result.put("resultMsg", "用户未登录");
			return ResponseUtil.formatResponseByRequest(jsonpCallback, result.toString());
		}
		List<Map<String, String>> listBound = new ArrayList<Map<String, String>>();
		int count = 0;
		try {
			log.info("同步统一用户绑定信息partnerId："+partnerId);
			long t1 = System.currentTimeMillis();
			count = this.ecCorporationBoundService.selectCorporationBoundCount(partnerId, "");
			log.error("AppMallAndElectroBindService->listBound：count" + count);
			long t2 = System.currentTimeMillis();
			listBound = this.ecCorporationBoundService.selectCorporationBound(partnerId, "");
			log.debug("获取绑定数据内容-partnerId="+partnerId+"->AppMallAndElectroBindService->listBound"+listBound.toString());
			long t3 = System.currentTimeMillis();
			log.info("=+=+=+=++=+=++=+=+==+    查询账户绑定数量消耗时间为：" + (t2-t1));
			log.info("=+=+=+=++=+=++=+=+==+    查询账户绑定列表消耗时间为：" + (t3-t1));
//			PARTNERID = partnerId;
//			log.info("新线程执行同步绑定信息逻辑1："+System.currentTimeMillis());
//			new Thread() {                     
//				public void run(){  
//			    	  syncQueryUSCBoundUserInfo();
//			      }                         
//			    }.start(); 
//			log.info("新线程执行同步绑定信息逻辑2："+System.currentTimeMillis());
		} catch (Exception e) {
			result.put("rcode", "1");
			result.put("resultMsg", "获取绑信息定列表出错");
			log.error("获取所有合作平台系统列表出错：" + e.getMessage());
			e.printStackTrace();
			return ResponseUtil.formatResponseByRequest(jsonpCallback, result.toString());
		}
		// 绑定信息
		result.put("rcode", "0");
		result.put("resultMsg", "获取绑信息定列表成功");
		result.put("listBoundCount", count);
		result.put("listBound", listBound);
		long endTime = System.currentTimeMillis();
		log.info("获取绑定信息列表所用的时间：startTiem="+startTime+"   endTime="+endTime+";时间差为："+(endTime-startTime));
		return ResponseUtil.formatResponseByRequest(jsonpCallback, result.toString());
	}
	
	/**
	 * @param token
	 *  		token
	 * @param jsonpCallback
	 * 			回传json串
	 * @param jsonStr
	 * 			json字符串
	 * @return
	 *  		jsonpCallback + "(" + jsonResult + ")"
	 */
	@Override
	public String getConsNoIntegralDetail(String token, String jsonpCallback, String jsonStr){
		JSONObject result = new JSONObject();
		JSONObject jsonParams = JSONObject.fromObject(jsonStr);
		log.info("获取用电户号对应的积分列表开始-------------------------------------------");
		try{
//			String userId = TokenUtil.getInstance(redisTem).getUserIdByToken(token);
			String userId = "1000305";//先使用固定值测试================================
			String consNo = "";
			if(jsonParams.containsKey("consNo")){
				consNo = jsonParams.getString("consNo");
			}else{
				log.error("参数consNo未传递");
				result.put("rcode", "1");
				result.put("resultMsg", "缺少参数consNo");
				return ResponseUtil.formatResponseByRequest(jsonpCallback, result.toString());
			}
			log.info("memberId传入的参数++++++++++++++++++++++++++++++++++++"+userId);
			log.info("consNo传入的参数++++++++++++++++++++++++++++++++++++"+consNo);
			
			if(StringUtils.isBlank(userId)){
				result.put("rcode", "1204");
				result.put("resultMsg", "用户未登录");
				return ResponseUtil.formatResponseByRequest(jsonpCallback, result.toString());
			}else{
				//获取分页参数==>curPage:第几页,pageSize:每页几条,totalRows:共几条=============aaa===========
//				Page page = LocalPageContext.setPageViaRequest(jsonParams.getString("curPage"), Integer.parseInt(jsonParams.getString("pageSize")));
				
				Map<String, String> map = new HashMap<String, String>();
				map.put("memberId", userId);
				map.put("consNo", consNo);
				
			    List<EcBpMemberDetail> list=new ArrayList<EcBpMemberDetail>();
			    list = ecCorporationBoundService.getConsNoIntegralDetail(map);
				
				List<EcBpMemberflow> listss = new ArrayList<EcBpMemberflow>();
				for (EcBpMemberDetail ecBpMemberDetail : list) {
					EcBpMemberflow ecBpMemberflow = new EcBpMemberflow();
					ecBpMemberflow.setBpValue(Long.valueOf(ecBpMemberDetail.getBpValue()));
					ecBpMemberflow.setBpYear(ecBpMemberDetail.getBpYear().shortValue());
					ecBpMemberflow.setCreatedTime(ecBpMemberDetail.getCreateTime());
					ecBpMemberflow.setOriginId(ecBpMemberDetail.getOriginId());
					ecBpMemberflow.setOriginRemark(ecBpMemberDetail.getOriginRemark());
					ecBpMemberflow.setMemberId(ecBpMemberDetail.getMemberId());
					listss.add(ecBpMemberflow);
				}
				result.put("rcode", "0");
//				result.put("totalSize", page.getTotalRows());
				result.put("totalSize", "10");//先使用固定的数据============aaa=================
				result.put("dataList", listss);
				log.info("listss==="+listss);
				return ResponseUtil.formatResponseByRequest(jsonpCallback, result.toString());
			}
		}catch (Exception e){
			log.error(e.getMessage(), e);
			result.put("rcode", "1");
			result.put("resultMsg", "接口调用失败");
			return ResponseUtil.formatResponseByRequest(jsonpCallback, result.toString());
		}
		
	}
	

}